package com.jeff.android.lab.module.thirdwidget.stickyheader;

import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.jeff.android.lab.BaseFragment;
import com.jeff.android.lab.R;
import com.jeff.utils.LogUtil;
import com.jeff.utils.ToastUtil;

import org.zakariya.stickyheaders.PagedLoadScrollListener;
import org.zakariya.stickyheaders.SectioningAdapter;
import org.zakariya.stickyheaders.StickyHeaderLayoutManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author: dingfeng
 * @date: 2019-11-28
 * @since: 1.0.0
 **/
public class StickyHeaderFragment extends BaseFragment {

    @BindView(R.id.rlv)
    RecyclerView mRecyclerView;

    NameListAdapter mNameListAdapter;

    @Override
    protected int getRootLayout() {
        return R.layout.thirdwidget_fragment_stickyheader;
    }

    @Override
    protected void initView() {
        StickyHeaderLayoutManager stickyHeaderLayoutManager = new StickyHeaderLayoutManager();
        mRecyclerView.setLayoutManager(stickyHeaderLayoutManager);

        stickyHeaderLayoutManager.setHeaderPositionChangedCallback(new StickyHeaderLayoutManager.HeaderPositionChangedCallback() {
            @Override
            public void onHeaderPositionChanged(int sectionIndex, View header, StickyHeaderLayoutManager.HeaderPosition oldPosition, StickyHeaderLayoutManager.HeaderPosition newPosition) {
                LogUtil.d("dingfeng", "sectionIndex="+ sectionIndex + "  oldPosition=" + oldPosition + "  newPosition=" + newPosition);

//                SectioningAdapter.HeaderViewHolder holder =  stickyHeaderLayoutManager.getFirstVisibleHeaderViewHolder(true);
//                NameListAdapter.HeaderHolder ivh = (NameListAdapter.HeaderHolder) holder;

//                final TextView btnFilter = header.findViewById(R.id.btn_filter);
//
//                if (newPosition == StickyHeaderLayoutManager.HeaderPosition.STICKY) {
//                    btnFilter.setVisibility(View.VISIBLE);
//                } else {
//                    btnFilter.setVisibility(View.GONE);
//                }

//                NameListAdapter.HeaderHolder ivh = (NameListAdapter.HeaderHolder) stickyHeaderLayoutManager.getFirstVisibleHeaderViewHolder(true);
//                ivh.btnFilter.setVisibility(View.VISIBLE);
//
                final TextView btnFilter = header.findViewById(R.id.btn_filter);

                if (newPosition == StickyHeaderLayoutManager.HeaderPosition.STICKY || newPosition == StickyHeaderLayoutManager.HeaderPosition.TRAILING) {
                    btnFilter.setVisibility(View.VISIBLE);
                }
                if (oldPosition == StickyHeaderLayoutManager.HeaderPosition.STICKY) {
                    btnFilter.setVisibility(View.INVISIBLE);
                }

            }
        });

        // listener
//        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
//            @Override
//            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
//                super.onScrollStateChanged(recyclerView, newState);
//
//                StickyHeaderLayoutManager manager = (StickyHeaderLayoutManager) recyclerView.getLayoutManager();
//                // 当不滑动时
//                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
//                    SectioningAdapter.ItemViewHolder itemViewHolder = manager.getLastVisibleItemViewHolder(true);
//
//                    if (itemViewHolder != null) {
//                        //获取最后一个完全显示的itemPosition
//                        int lastItemPosition = itemViewHolder.getAdapterPosition();
//
//                        // 判断是否滑动到了最后一个item，并且是向上滑动
//                        if (lastItemPosition == (manager.getItemCount() - 1)) {
//                            //加载更多
//                            ToastUtil.show(mContext, "加载更多");
//
//                            loadMore();
//                        }
//                    }
//                }
//            }
//
//            @Override
//            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
//                super.onScrolled(recyclerView, dx, dy);
//                // 大于0表示正在向上滑动，小于等于0表示停止或向下滑动
//                isSlidingUpward = dy > 0;
//            }
//        });

        mRecyclerView.addOnScrollListener(new PagedLoadScrollListener(stickyHeaderLayoutManager) {
            @Override
            public void onLoadMore(int page, LoadCompleteNotifier loadComplete) {
                ToastUtil.show(mContext, "加载更多");

                loadMore();
            }
        });
    }


    //用来标记是否正在向上滑动
    private boolean isSlidingUpward = false;

    @Override
    protected void initData() {
        List<Name> names = new ArrayList<>();

        names.add(new Name("D", "Ding", "Feng"));
        names.add(new Name("D", "Duang", "Feng"));
        names.add(new Name("A", "Apple", "Feng"));
        names.add(new Name("X", "Xue", "Feng"));
        names.add(new Name("X", "Xia", "Feng"));
        names.add(new Name("H", "Huang", "Feng"));
        names.add(new Name("D", "Deng", "Feng"));
        names.add(new Name("F", "Fing", "Feng"));
        names.add(new Name("F", "Fang", "Feng"));
        names.add(new Name("F", "Fqng", "Feng"));
        names.add(new Name("F", "Foka", "Feng"));
        names.add(new Name("J", "Jack", "D"));
        names.add(new Name("J", "Jack", "D"));
        names.add(new Name("J", "Jack", "D"));
        names.add(new Name("J", "Jack", "D"));

        mNameListAdapter = new NameListAdapter(names);
        mRecyclerView.setAdapter(mNameListAdapter);
        mNameListAdapter.notifyAllSectionsDataSetChanged();

    }

    private void loadMore() {
        List<Name> names = new ArrayList<>();
        names.add(new Name("Z", "Ding", "Feng"));
        names.add(new Name("Z", "Duang", "Feng"));
        names.add(new Name("Z", "Apple", "Feng"));
        names.add(new Name("Z", "Ding", "Feng"));
        names.add(new Name("Z", "Duang", "Feng"));
        names.add(new Name("Z", "Apple", "Feng"));
        names.add(new Name("D", "DDDDDDDDDD", "Feng"));

        mNameListAdapter.addData(names);
    }

}
